<?php
/**
 * Production Model
 * 
 * @author CRIA Groep 1 <project@markvink.nl>
 * @version 1.0
 * @package models
 */
class Production extends AppModel 
{
	/**
    * @var string Model name (faster in PHP 4)
    */
	var $name = 'Production';
	
	/**
    * @var string displays field in select box
    */
	var $displayField = 'id';
	
	/**
    * @var array belongsTo relations
    */	
	var $belongsTo = array(
        'Measurement' => array(
            'className' => 'Measurement'
        ),
		'Productiontype' => array(
            'className' => 'Productiontype'
        ),
	);
	
	/**
	 * Function update
	 * 
	 * Update production data
	 * @param integer $measurement measurement_id
	 * @param integer $productiontype productiontype_id
	 * @param integer $amount amount
	 * @return boolean
	 */
	function update($measurement, $productiontype, $amount) {
		$production = $this->find('first', array(
			'conditions' => array(
				'Production.productiontype_id' => $productiontype,
				'Production.measurement_id' => $measurement,
			)
		));
		if(empty($production)) {
			$this->create();
			$data = array('Production' => array(
				'productiontype_id' => $productiontype,
				'measurement_id' => $measurement,
				'amount' => $amount,
			));
			return $this->save($data);
		} else {
			$this->read(null, $production['Production']['id']);
			return $this->saveField('amount', $amount);
		}
	}
}
